#include "my_wifi.h"

static TaskHandle_t thread_wifi;
static wifi_config_t wifi_config;
WIFI_SYS wifi_sys;
char passwd[64];
char ssid[100];
size_t len1;
size_t len2;
u8 tcp_frist_init =0;




void set_tcp_frist_init(u8 flag)
{
    tcp_frist_init = flag;
}

u8 get_tcp_frist_init()
{
    return tcp_frist_init;
}


EM_WIFI_SSP wifi_logic_get_ssp()
{
    return wifi_sys.wifi_ssp;
}

void wifi_logic_set_ssp(EM_WIFI_SSP wifi_ssp)
{
    wifi_sys.wifi_ssp = wifi_ssp;
    LOG_INFO("set  wifi ssp %d\n",wifi_sys.wifi_ssp);
}

EM_WIFI_SSP wifi_logic_get_last_ssp()
{
    return wifi_sys.last_wifi_ssp;
}

void wifi_logic_set_last_ssp(EM_WIFI_SSP last_wifi_ssp)
{
    wifi_sys.last_wifi_ssp =last_wifi_ssp;
    LOG_INFO("set  last wifi ssp %d\n",wifi_sys.last_wifi_ssp);
}

void come_to_match_network()
{   
    LOG_INFO("4444\n");
    if (BULFI_OPEN !=wifi_logic_get_ssp()) //如果当前不是配网状态，则进入配网状态
    {
        wifi_logic_set_ssp(BULFI_OPEN);
        LOG_INFO("come_to_match_network()\n");
    }
    
}

//WIFI蓝牙关闭状态，对应节能模式
void wifi_power_off()
{
    if(wifi_logic_get_last_ssp() != wifi_logic_get_ssp())
    {
        wifi_logic_set_last_ssp(wifi_logic_get_ssp());
        LOG_INFO("wifi_power_off()\n");
    }
}

//bulfi开启状态
void blufi_open()
{
    static int time;
    if(wifi_logic_get_last_ssp() != wifi_logic_get_ssp())
    {   
        LOG_INFO("INTO blufi_open()\n");
        wifi_logic_set_last_ssp(wifi_logic_get_ssp());
        u8 ret;
        ret = esp_blufi_controller_init(); //使能蓝牙控制器
        if (ret) 
        {
            printf("%s BLUFI controller init failed: %s\n", __func__, esp_err_to_name(ret));
            return;
        }
        ret = esp_blufi_host_and_cb_init(&example_callbacks);
        if (ret) 
        {
            BLUFI_ERROR("%s initialise failed: %s\n", __func__, esp_err_to_name(ret));
            return;
        }
        time = arch_os_ms_now();
        set_flag(0);
    }

    if(get_blufi_flag()==0)
    {
        net_light_config.waiting();
    }
    else if(get_blufi_flag()==1)
    {
        net_light_config.connectting();
    }
    

    if(arch_os_ms_elapsed(time) >=(1*60*1000))//计时1分钟
    {
        LOG_INFO("blufi connect overtime\n");
        time = arch_os_ms_now();
        wifi_logic_set_ssp(BULFI_CONNECT_OUT);
    }
}

//blufi连接超时
void blufi_connect_out()
{   
    static int time;
    static u8 flag;
    if(wifi_logic_get_last_ssp() != wifi_logic_get_ssp())
    {
        wifi_logic_set_last_ssp(wifi_logic_get_ssp());
        LOG_INFO("blufi_connect_out()\n");
        esp_blufi_host_deinit();
        esp_blufi_controller_deinit();
        time =arch_os_ms_now();
        flag =0;
    }

    if(arch_os_ms_elapsed(time)>=(5*1000)) //3s
    {
        flag =1;
        time =arch_os_ms_now();
        set_flag(1);
    }

    if (0==flag)
    {
        net_light_config.fail();
    }
    
}

//WIFI连接中
void wifi_connecting()
{
    static int time;
    if(wifi_logic_get_last_ssp() != wifi_logic_get_ssp())
    {
        LOG_INFO("INTO wifi_connecting()\n");
        wifi_logic_set_last_ssp(wifi_logic_get_ssp());
        esp_wifi_connect();
        set_flag(1);
    }
    
    if (arch_os_ms_elapsed(time) >= 10*1000) //10s循环一次
    {
        esp_wifi_disconnect();
        esp_wifi_set_config(WIFI_IF_STA,&wifi_config);
        esp_wifi_connect();
        LOG_INFO("esp_wifi_disconnect() %d\n",time);
        time = arch_os_ms_now();
    }
}


//WIFI连接完成
void wifi_connected()
{

    static int time;
    static int time1;
    static u8 flag;
    if(wifi_logic_get_last_ssp() != wifi_logic_get_ssp())
    {   
        wifi_logic_set_last_ssp(wifi_logic_get_ssp());
        LOG_INFO("wifi connected\n");
        if(get_tcp_frist_init() == 0)
        {
            task_lwip_tcp_init();
            set_tcp_frist_init(1);
        }
        time = arch_os_ms_now();
        time1 =arch_os_ms_now();
        flag =0;
    }

    if (arch_os_ms_elapsed(time)>= (10*1000))  //10s判断一次
    {
        if(0==get_wifi_connect_status())
        {
            wifi_logic_set_ssp(WIFI_CONNECTING);
        }
        time = arch_os_ms_now();
    }

    if((arch_os_ms_elapsed(time1)>=(5*1000)) &&(get_flag() != 1)) //3s
    {
        flag =1;
        time1 =arch_os_ms_now();
        set_flag(1);
    }

    if ((0==flag)&&(get_flag() != 1))
    {
        net_light_config.success();
    }

}

WIFI_MODE wifi_mode[NUM_WIFI_SSP] =
{
    {wifi_power_off}, //0
    {blufi_open}, //1
    {blufi_connect_out}, //2
    {wifi_connecting}, //3
    {wifi_connected}, //4
};

void wifi_mode_handle()
{
    if(wifi_mode[wifi_logic_get_ssp()].handle)
    {
        wifi_mode[wifi_logic_get_ssp()].handle(); //进入函数进行处理
    }
    else
    {
        LOG_ERROR("cur ssp handle is not defined\n");
    }
}



void my_wifi_init()
{
    wifi_enable(); //wifi使能开启
    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
    wifi_logic_set_last_ssp(NUM_WIFI_SSP);

    if((driver_nvs_part_read_str(WIFI_PSWD,passwd,&len1) == ESP_OK)&&(driver_nvs_part_read_str(WIFI_SSID,ssid,&len2) == ESP_OK))
    {
        wifi_logic_set_ssp(WIFI_CONNECTING); //进入自动配网状态
        strncpy((char *)wifi_config.sta.ssid,ssid,len2);
        LOG_INFO("sta_config.sta.ssid %s\n",wifi_config.sta.ssid);
        strncpy((char *)wifi_config.sta.password,passwd,len1);
        LOG_INFO("sta_config.sta.passwd %s\n",wifi_config.sta.password);
    }
    else
    {
        LOG_INFO("wifi first connet\n");
        wifi_logic_set_ssp(BULFI_OPEN);
    }
    

}

void wifi_logic_loop(void *pvParameters)
{
    my_wifi_init();
    while (1)
    {   
        wifi_mode_handle();
        vTaskDelay(pdMS_TO_TICKS(WIFI_LOOP_SPEED_MS)); //延时50ms
    }
    
}

void task_wifi_init()
{
     xTaskCreate(wifi_logic_loop,"wifi_logic_loop",WIFI_LOGIC_SIZE,NULL,TASK_PROP_NET,&thread_wifi);
}



